{% extends "base.html" %}
{% load adminmedia %}
{% block extrastyle %}
  {{ block.super }}
  <link rel="stylesheet" type="text/css" href="{{MEDIA_URL}}print_ticket.css" media="print" />
{% endblock %}
{% block extrajs %}
<script type="text/javascript" src="{{ MEDIA_URL }}javascript/modify.js"></script>
<script type="text/javascript" src="{{ MEDIA_URL }}javascript/etiquette.js"></script>
<script type="text/javascript" src="{{ MEDIA_URL }}js/DijitRelatedObjectLookups.js"></script>
{% endblock %}

{% block title %}Ticket numéro {{ form.instance.id }}{% endblock %}
{% block content_title %}Ticket numéro {{ form.instance.id }}{% endblock %}
{% block image %}<img src="{{ MEDIA_URL }}images/oxygen/folder-open.png" />{% endblock %}
{% block content %}
{{ form.non_field_errors }}
<form enctype="multipart/form-data" action="{% url ticket_modify form.instance.id %}" method="post" id="ticket_form">
<div id="parent">
{% csrf_token %}
{% block content_ticket %}
<fieldset id="fieldset_general" class="inline form">
  <legend>Général</legend>
  <div>{{ form.category.label_tag }} {{ form.category }}</div>
  <div>{{ form.project.label_tag }} {{ form.project }}</div>
  <div>{{ form.priority.label_tag }} {{ form.priority }}</div>
  <div>{{ form.state.label_tag }} {{ form.state }}</div>
</fieldset>

<script type="text/javascript">
  if (! dojo.isIE ) {
    changeClassOnScroll(117, '#fieldset_general', 'inline form', 'inline fieldset_fixed');
  }
var lock_url = '{% url lock_ajax_lock form.instance.id %}?model=ticket.Ticket&last_timestamp_field=last_modification&timestamp={{ form.instance.last_modification|date:"U" }}';
var lock_settings = {{ lock_settings|safe }};
</script>

<fieldset class="block form">
  <legend>Contact</legend>
  <table>
    <tr>
        <td class="large">{{ form.client.label_tag }}</td><td>{{ form.client }}
            <a href={% url list_hosts form.instance.client.id %}>
                <img alt="postes" src="{{ MEDIA_URL }}images/oxygen/computer_small.png" /></a></td>
        <td class="large right">{{ form.assigned_to.label_tag }}</td><td>{{ form.assigned_to }}</td>
    </tr>
    <tr>
        <td class="large">{{ form.contact.label_tag }}</td><td>{{ form.contact }}</td>
        <td class="large right"><label for="id_opened_by">Ouvert par</label></td><td>{{ form.instance.opened_by }} le {{ form.instance.date_open|date:"d/m/Y à H:i" }}</td>
    </tr>
    <tr>
        <td class="large">{{ form.telephone.label_tag }}</td><td>{{ form.telephone }}
            <div dojoType="dijit.form.Button" onClick="ajaxMenu2D('{% url ajax_load_telephone %}', 'client_id='+dijit.byId('id_client'), this.id, 'telephone_menu', 'telephones', 'id_contact', 'id_telephone');" id="telephoneContainer">Présélection</div>
        </td>
        <td class="large right">Dernière modif.</td><td>le {{ form.instance.last_modification|date:"d/m/Y à H:i" }}</td>
    </tr>
    <tr>
        <td class="large">&nbsp;</td><td></td>
        <td class="large right">
            {% if form.instance.validated_by %}
                <strong>Validé par</strong></td><td>{{ form.instance.validated_by }}
            {% else %}
                {% if perms.ticket.can_validate_ticket %}
                    <td colspan="2"><button type="submit" dojoType="dijit.form.Button" name="_validate-ticket" value="Valider ce ticket">Valider ce ticket</button></td>
                {% else %}
                    <strong>Validation:</strong></td><td><strong>En attente</strong>
                {% endif %}
            {% endif %}
        </td>
    </tr>
  </table>
</fieldset>

<fieldset class="block form print_content">
  <legend>Contenu</legend>
  <table class="print_title">
    <tr>
        <th class="large print_bottom_padding">{{ form.title.label_tag }} {{ form.title.errors }}</th>
        <td class="large right print_bottom_padding">{{ form.title }}</td>
    </tr>
    <tr>
        <th>{{ form.text.errors }}</th>
        <td class="right">{{ form.text }}</td>
    </tr>
    <tr>
        <th>{{ form.keywords.label_tag }}</th>
        <td class="right">{{ form.keywords }}</td>
    </tr>
  </table>
</fieldset>

{% with form as form_to_comment %}
<fieldset class="block form" style="padding: 0;">
  <legend>Réponses</legend>
{% include "ticket/comment.html" %}
</fieldset>
{% endwith %}

<fieldset class="block form no_print">
    <legend>Appels du client</legend>
    {% with form.instance.ticketappel_set.all as appels %}
    {% if appels %}
    <ul>
        {% for a in appels %}
        <li>Le {{ a.date|date:"d/m/Y à H:i" }} par {{ a.user.username }}</li>
        {% endfor %}
    </ul>
    {% endif %}
    {% endwith %}
    <div>{{ form.appel }} {{ form.appel.label_tag }}</div>
</fieldset>

<fieldset class="block form no_print">
  <legend>Planification</legend>
  <table>
    <tr>
        <th class="large">{{ form.calendar_start_time.label_tag }}</th>
        <td class="right">{{ form.calendar_start_time }}</td>
    </tr>
    <tr>
        <th class="large">{{ form.calendar_end_time.label_tag }}</th>
        <td class="right">{{ form.calendar_end_time }}</td>
    </tr>
  </table>
</fieldset>

<fieldset class="block form no_print">
    <legend>Fichiers joint</legend>
    <table>
        {% for file in form.instance.ticketfile_set.all %}
        <tr>
            <td><a href="{% url ticket_get_file file.pk %}">{{ file.filename }}</a></td>
        </tr>
        {% endfor %}
        <tr>
            <td>{{ form.file }}</td>
        </tr>
    </table>
</fieldset>

<fieldset class="block form no_print">
    <legend>Bon de commande</legend>
    {% if bondecommandes %}
    <ul>
    {% for bdc in bondecommandes %}
        <li{% if bdc.is_closed %} style="text-decoration:line-through"{% endif %}>
            {% if perms.bondecommande.change_bondecommande %}
              <a href="{% url admin:bondecommande_bondecommande_change bdc.id %}">{{ bdc }}</a>
            {% else %}
              {{ bdc }}
            {% endif %}
            le {{ bdc.date_creation }}: {{ bdc.comment }}
            
            {% if bdc.bondecommandefile_set.all %}<ul>{% for f in bdc.bondecommandefile_set.all %}
            <li>{{ f }}</li>
            {% endfor %}</ul>{% endif %}
        </li>
    {% endfor %}
    </ul>
    {% endif %}
    {% if perms.bondecommande.add_bondecommande %}
    <h3>Lier un bon de commande:</h3>
    {{ form.bondecommande }}
        <a id='add_id_bondecommande' class="add-another" href="{% url admin:bondecommande_bondecommande_add %}?client={{ form.instance.client.id }}" onclick="return showAddAnotherPopup(this);">
            <img width="10" height="10" alt="Ajouter un autre" src="{% admin_media_prefix %}img/admin/icon_addlink.gif" />
        </a>
    {% endif %}
</fieldset>
<hr />

{% endblock content_ticket %}
</div><!-- parent -->

<script type="text/javascript">
    if (! dojo.isIE) {
      var last_focused;
      var fl;
      var buttons;
      childManagement();
    }
</script>

{% block content_childs %}
<div id="div_childs" class="childs">
{{ child_formset.management_form }}
{% for cf in child_formset.forms %}
{% include "ticket/child.html" %}
{% endfor %}
</div>
{% endblock content_childs %}


<div id="save_buttons">
    {% with form.instance.get_current_alarm as alarm %}
    <div class="left_buttons">
        <div dojoType="dijit.form.Button" id="alarm_button" 
            iconClass="dijitEditorIcon claritick{% if not alarm %}No{% endif %}TicketAlarm" 
            onClick="dijit.byId('alarm_dialog').show();">Alarme</div>
        <div dojoType="dijit.Tooltip" connectId="alarm_button">
            <h3>Alarme</h3>
            <p>En mettant une alarme sur ce ticket, il sera marqué comme urgence.</p>
        </div>
    </div>
    {% if perms.etiquette_printer.print_etiquettetemplate %}
    <div class="left_buttons">
        <div dojoType="dijit.form.Button" id="etiquette_button" 
            iconClass="dijitEditorIcon claritickEtiquettePrintIcon" 
            onClick='get_print_dialog("{% url etiquette_printer.print %}", "ticket", "Ticket", "{{ form.instance.id }}").show();'>Etiquette</div>
        <div dojoType="dijit.Tooltip" connectId="etiquette_button">
            <h3>Etiquette</h3>
            <p>Permet d'imprimer des étiquettes relatives à ce ticket. (Identification de carton, matériel, ...)</p>
        </div>
    </div>
    {% endif %}
    
    <div dojoType="dijit.Dialog" id="alarm_dialog" title="{{ alarm.title_string|default:"Nouvelle Alarme" }}">
        <label for="id_alarm">Raison </label><br /><textarea rows="12" cols="32" id="id_alarm" name="alarm_reason">{{ alarm.reason }}</textarea>
        <div style="text-align: center;">
            <button dojoType="dijit.form.Button" type="submit" onClick="dojo.byId('id_alarm').value=''; setTicketAlarm('{% url ajax_set_alarm form.instance.id %}');">Annuler l'alarme</button>
            <button dojoType="dijit.form.Button" onClick="setTicketAlarm('{% url ajax_set_alarm form.instance.id %}');">Valider</button>
        </div>
    </div>
    <script type="text/javascript">
        {% if alarm %}
        dojo.addOnLoad(function () {
            dijit.byId('alarm_dialog').show();
        });
        {% endif %}
    </script>
    {% endwith %}

    <button dojoType="dijit.form.Button" iconClass="dijitEditorIcon dijitEditorIconSave" type="submit" name="savebutton" value="save">Enregistrer</button>
      <button dojoType="dijit.form.Button" iconClass="dijitEditorIcon dijitEditorIconSave" type="submit" name="savebutton" value="new">Enregistrer et ajouter un nouveau</button>
    <button dojoType="dijit.form.Button" iconClass="dijitEditorIcon dijitEditorIconSave" type="submit" name="savebutton" value="return" class="default">Enregistrer et retourner à la liste</button>
    {% if perms.ticket.can_add_child %}
    <button dojoType="dijit.form.Button" iconClass="dijitEditorIcon claritickNewChild" onClick="loadChild('{% url ajax_load_child form.instance.id %}', {% if perms.ticket.add_ticket_full %}true{% else %}false{% endif %}, {{ settings.TICKET_STATE_NEW }}, {{ settings.TICKET_STATE_CLOSED }}, {{ settings.TICKET_STATE_ACTIVE }});" id="button_add_child">Ajouter un fils</button>
    {% endif %}

    <div class="right_buttons">
    {% if form.instance.ticketmailtrace_set.count %}
    <div dojoType="dijit.form.Button" iconClass="dijitEditorIcon claritickTicketMailAction" id="id_tmt_button" onClick="viewTicketMailTraceDialog('{% url ajax_load_ticketmailtrace form.instance.id %}');">Trace</div>
    <div dojoType="dijit.Tooltip" connectId="id_tmt_button">
        <h3>Ticket Mail Trace</h3>
        <p>Liste les mails envoyés pour ce ticket</p>
    </div>

    <script type="text/javascript">
        dojo.require('dijit.layout.AccordionContainer');
        var ticketmailtrace = null;
    </script>
    {% endif %}

    {% if perms.ticket.can_delete_tma and form.instance.ticketmailaction_set.count %}
    <div dojoType="dijit.form.DropDownButton" iconClass="dijitEditorIcon claritickTicketMailAction" id="dijit_tmas">
        <span>{{ form.instance.ticketmailaction_set.count }}</span>
        <div dojoType="dijit.Menu">
            <div dojoType="dijit.MenuItem" iconClass="dijitEditorIcon dijitEditorIconDelete" onClick='deleteTmas("{% url ajax_delete_tma form.instance.id %}", dijit.byId("dijit_tmas"), 0);'>Supprimer tout</div>
            {% for tma in form.instance.ticketmailaction_set.all %}
            <div dojoType="dijit.MenuItem" iconClass="dijitEditorIcon dijitEditorIconDelete" onClick='deleteTmas("{% url ajax_delete_tma form.instance.id %}", this, {{ tma.id }})' style="display: block;">{{ tma.reasons|join:"<br />" }}</div>
            {% endfor %}
        </div>
    </div>

    <div dojoType="dijit.Tooltip" connectId="dijit_tmas">
        <h3>Ticket mail action</h3>
        <p>Un mail va bientôt être envoyé en raison des actions récentes qu'il y a eu sur ce ticket</p>
    </div>

    {% endif %}
    </div><!-- right_buttons -->

</div>

</form>

    <div dojoType="dijit.Dialog" id="lock_dialog" title="Ticket verrouillé">
        <p id="lock_dialog_content"></p>
        <div style="text-align: center;">
            <div dojoType="dijit.form.Button" onClick="dijit.byId('lock_dialog').hide();">Ignorer</div>
            <div dojoType="dijit.form.Button" onClick="refresh_page();">Recharger</div>
        </div>
    </div>

{% endblock content %}
